<?php
namespace app\staff\controller;
use think\Request;
use app\common\controller\Common;
class Order extends Common
{
	protected $order = 'order.order_id desc';
	protected $keyword_search = ['order.order_sn','order.username','order.user_sn','order.phone','order.order_money','order.stay_money','goods.category_id','goods.cat_id','goods.brand_name','goods.memory_name','goods.attribute_name','goods.colour_name','goods.contract_sn'];
	/**
	 * 员工销售记录
	 */
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				$list = model('user/Order')->orderList($where, $order, $row, $search);
				$count = model('user/Order')->orderCount($where, $order, $search);
				
				$consum = model('OrderCount')->allOrderGoods($where, $order, $search);
				$this->success(['list'=>$list, 'count'=>$count, 'consum'=>$consum]);
			}
			/*@产品分类*/
			$cate = model('Category')->where('fid',0)->select();
			$this->assign('cate',$cate);
			return $this->fetch('');
		}
	}
	/**
	* 员工销售业绩
	*/
	public function statistics()
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
	}
	/******************************************************** 私有 *********************************************************/
	/**
	 * 列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = [];
		/*@已支付订单*/
		// $where['order.state'] = 1;
		/*@登录者id*/
		if(isset($data['ids']) && $data['ids'] != ''){
			$where['order.admin_id'] = $data['ids'];
		}else{
			switch(session('admin.admin_type')){
				case 2: $where['order.admin_id'] = session('admin.admin_id');break;
				default: return false;break;
			}
		}
		
		/*@产品分类*/
		if(isset($data['category_id']) && $data['category_id'] != ''){
			$where['goods.category_id'] = $data['category_id'];
		}
		/*@产品子分类*/
		if(isset($data['cat_id']) && $data['cat_id'] != ''){
			$where['goods.cat_id'] = $data['cat_id'];
		}
		/*@产品品牌*/
		if(isset($data['brand_name']) && $data['brand_name'] != ''){
			$where['goods.brand_name'] = $data['brand_name'];
		}
		/*@产品内存*/
		if(isset($data['memory_name']) && $data['memory_name'] != ''){
			$where['goods.memory_name'] = $data['memory_name'];
		}
		/*@产品属性*/
		if(isset($data['attribute_name']) && $data['attribute_name'] != ''){
			$where['goods.attribute_name'] = $data['attribute_name'];
		}
		/*@产品颜色*/
		if(isset($data['colour_name']) && $data['colour_name'] != ''){
			$where['goods.colour_name'] = $data['colour_name'];
		}
		/*@时间区间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['order.addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['order.addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['order.addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}